package com.jy.wms.service;

import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.dao.WmsOrderChannelMapper;
import com.jy.wms.pojo.WmsOrderChannel;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
    * @ClassName: WmsOrderChannelService.java
    * @Description: TODO(这里用一句话描述这个类的作用)
    * @Author Jane
    * @Date Mon Dec 25 10:44:15 CST 2017
    */

    @Service
    public class WmsOrderChannelService extends BaseService<WmsOrderChannelMapper,WmsOrderChannel> {

     /**
     * 插入一条记录
     * @param entity 实体对象
     * @return int
     */
    public Integer baseInsert(WmsOrderChannel entity){
        int count=dao.countSumByFjd(entity.getFjd());
        entity.setWz(String.valueOf(count+1));//计算此节点在父节点的位置
        entity.setSq("1");
         return dao.baseInsert( entity );
    }

   /**
    * 插入一条记录（匹配有值的字段）
    * @param entity 实体对象
    * @return int
    */
    public Integer baseInsertSelective(WmsOrderChannel entity){
         return dao.baseInsertSelective( entity);
    }

   /**
    * 删除（根据主键ID删除）
    * @param id 主键
    * @return int
    */
    public Integer baseDeleteByPrimaryKey(String id){
         return dao.baseDeleteByPrimaryKey( id);
    }

   /**
    * 删除（根据ID 批量删除）
    * @param idList 主键ID列表
    * @return int
    */
    public Integer baseDeleteBathPrimaryKeys(List<String> idList){
         return dao.baseDeleteBathPrimaryKeys( idList );
    }

    /**
    * 分页查询
    * @param sshzid 所属货主ID
     *@param ckid 所属仓库ID
    * @return Page<WmsOrderChannel>
    */
    public List<WmsOrderChannel> baseSelectPage(String sshzid, String ckid){
         List<WmsOrderChannel> orderChannels=new ArrayList<>();
        List<WmsOrderChannel> order=dao.baseSelectPage(sshzid,ckid,"0");
        for(WmsOrderChannel orderChannel:order){
            orderChannel.setLable(orderChannel.getMc());
           orderChannels.add(orderChannel);
           orderChannel.setChildren(children(orderChannel.getId()));
        }
        return orderChannels;
    }
    /*
    根据父节点添加子节点
     */
    public List<WmsOrderChannel> children(String fjd){
        // 子菜单
        List<WmsOrderChannel> childList = new ArrayList<WmsOrderChannel>();
        List<WmsOrderChannel> childrens=dao.seachChildrenByFjd(fjd);
        for (WmsOrderChannel channel: childrens) {
            // 遍历所有节点，将父菜单id与传过来的id比较
                    channel.setLable(channel.getMc());
                    childList.add(channel);
            }
        // 把子菜单的子菜单再循环一遍
        for (WmsOrderChannel child : childList){
                child.setChildren(children(child.getId()));
        }
        if(childList.size()==0){
            return null;
        }
        return childList;
    }
   /**
    * 查询（根据主键ID查询）
    * @param id 主键ID
    * @return WmsOrderChannel
    */
    public WmsOrderChannel baseSelectByPrimaryKey(String id ){
         return dao.baseSelectByPrimaryKey( id );
    }

   /**
    * 查询（根据ID 批量查询）
    * @param idList 主键ID列表
    * @return list
    */
    public Integer baseSelectBathPrimaryKeys(List<WmsOrderChannel> idList ){
         return dao.baseSelectBathPrimaryKeys( idList );
    }

   /**
    *修改 （匹配有值的字段）
    * @param entity 主键ID列表
    * @return int
    */
    public void baseUpdateByPrimaryKeySelective( WmsOrderChannel entity)throws ServiceException {
        Integer count=dao.countMcAndBh(entity.getMc(),entity.getCode(),entity.getId());
        if(count>0){
            throw new ServiceException("渠道编码或名称重复，编辑失败");
        }else{
            Integer flag=dao.baseUpdateByPrimaryKeySelective( entity);
            if(flag<=0){
                throw new ServiceException("渠道编辑失败");
            }
        }
    }
        /**
         * 订单渠道code自动生成
         *
         */
        public String code(){
                int machineId = 1;//最大支持1-9个集群机器部署
                int hashCodeV = UUID.randomUUID().toString().hashCode();
                if(hashCodeV < 0) {//有可能是负数
                    hashCodeV = - hashCodeV;
                }
                String code="qd"+ String.format("%015d", hashCodeV);
                 Integer count=  dao.countCode(code);
            if(count==0){
                return code;
            }else {
               String num= code();
               return num;
            }
        }
   /**
    *修改（根据主键ID修改）
    * @param entity 主键ID列表
    * @return int
    */
    public Integer baseUpdateByPrimaryKey(WmsOrderChannel entity){
         return dao.baseUpdateByPrimaryKey( entity);
    }
        /**
         *修改（根据主键ID修改）
         * @param mc 渠道名称
         *  @param code 渠道编号
         * @return int
         */
    public Integer countMcAndBh(String mc, String code, String id){return dao.countMcAndBh(mc,code,id);}
}
